const path = require("path");
const IS_PROD = process.env.NODE_ENV === "production";
const FileManagerPlugin = require("filemanager-webpack-plugin");
function resolve (dir) {
  return path.join(__dirname, dir); // path.join(__dirname)设置绝对路径
}
module.exports = {
  transpileDependencies: true,

  // 开发服务
  devServer: {
    // overlay: {
    //   // 让浏览器 overlay 同时显示警告和错误
    //   warnings: false,
    //   errors: false
    // },
    port: 8080
    //   before: app => {
    //     if (setting.dev_mock) {
    //       const mock_server = require("./src/api/mock-server.js");
    //       mock_server(app);
    //     }
    //   },
    // before: require("./src/api/mock-server.js"),
    // proxy: {
    //   "/api": {
    //     target: "https://xxxxx.com", // 目标代理接口地址
    //     secure: false, // 如果是https接口的话，需要配置这个参数
    //     changeOrigin: true, // 开启代理，在本地创建一个虚拟服务端
    //     // ws: true, // 是否启用websockets
    //     pathRewrite: {
    //       "^/api": "" // 需要rewrite重写的,
    //     }
    //   }
    // }
  },
  // 对webpack的别名手写
  configureWebpack: {
    // resolve: {
    //   alias: {
    //     // 告诉 webpack，＠符号表示 src 这一层目录
    //     "@": resolve("./src")
    //   }
    // },
    // hash、chunkhash、contenthash
    output: {
      filename: IS_PROD ? "js/[name]_[contenthash:8].js" : "js/[name]_[hash:8].js",
      chunkFilename: IS_PROD ? "js/[name]_[contenthash:8].js" : "js/[name]_[hash:8].js"
    },
    // 直接打包成zip包
    plugins: [
      new FileManagerPlugin({
        events: {
          onEnd: {
            delete: ["./dist/dist.zip"],
            archive: [{ source: "./dist", destination: "./dist/dist.zip" }]
          }
        }
      })
    ],

    // 直接在这写上名字就能用的cdn
    externals: {
      // vue: "Vue",
      // vuex: "Vuex",
      // axios: "axios",
      // "vue-router": "VueRouter",
      // vant: "vant",
      // echarts: "echarts"
    }

  },
  chainWebpack: config => {
    // 设置别名(方便引入)
    config.resolve.alias
      .set("@", resolve("./src"))
      .set("assets", resolve(".src/assets"))
      .set("api", resolve("./src/api"))
      .set("components", resolve("./src/components"))
      .set("icons", resolve(".src/icons"))
      .set("store", resolve(".src/store"))
      .set("utils", resolve(".src/utils"))
      .set("filters", resolve(".src/filters"))
      .set("views", resolve(".src/views"));
  },
  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "less",
      patterns: [
        // 全局变量路径，不能使用路径别名
        path.resolve(__dirname, "./src/assets/styles/variables.less")
      ]
    }
  }
};
